define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form, undefined) {
    if(Config.ids == '' || Config.ids == null){
        Config.ids = 0;
    }
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'feditionorderquantity/index/?ids=' + Config.ids,
                    add_url: 'feditionorderquantity/add',
                    edit_url: 'feditionorderquantity/edit',
                    del_url: 'feditionorderquantity/del',
                    multi_url: 'feditionorderquantity/multi',
                    import_url: 'feditionorderquantity/import',
                    table: 'fedition_orderquantity',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        //{field: 'id', title: __('Id')},
                        {field: 'order_time', title: __('订单添加时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        // {field: 'order_id', title: __('Order_id'), operate: 'LIKE'},
                        // {field: 'product_id', title: __('Product_id'), operate: 'LIKE'},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},

                        {field: 'user_id', title: __('商品归属人'), formatter:Table.api.formatter.flag, operate: 'LIKE'},
                        {field: 'quantity', title: __('Quantity'), operate: 'LIKE'},
                        //{field: 'variant_title', title: __('Variant_title'), operate: 'LIKE'},
                        //{field: 'sku', title: __('Sku'), operate: 'LIKE'},
                        //{field: 'variant_id', title: __('Variant_id'), operate: 'LIKE'},
                        // {field: 'material', title: __('变体价格'), operate: 'LIKE'},
                        {field: 'price', title: __('单价'), operate: false},
                        {field: 'zmaterial', title: __('总价'), operate: false},
                        {field: 'material', title: __('商品成本'), operate: false},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('该商品SKU库'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('该商品SKU库'),
                                    title: __('该商品SKU库'),
                                    extend: 'data-area=\'["90%","90%"]\'',
                                    classname: 'btn btn-sm btn-success btn-dialog',
                                    icon: 'fa fa-try fa-fw',
                                    url: 'feditionordersku/index?product_id={product_id}'
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                $("#zproduct").text(data.Statistics.zproduct);
                $("#zmaterial").text(data.Statistics.zmaterial);
                $("#zquantity").text(data.Statistics.zquantity);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        doubt: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'feditionorderquantity/doubt',
                    add_url: 'feditionorderquantity/add',
                    edit_url: 'feditionorderquantity/edit',
                    del_url: 'feditionorderquantity/del',
                    multi_url: 'feditionorderquantity/multi',
                    import_url: 'feditionorderquantity/import',
                    table: 'fedition_orderquantity',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},

                        {field: 'accounturl_name', title: __('店铺'), operate: false},
                        //{field: 'admin_name', title: __('店铺管理人'), operate: false},
                        //{field: 'admin_name2', title: __('特殊归属人'), operate: false},
                        {field: 'order_name', title: __('存疑订单'), operate: 'LIKE'},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'quantitys', title: __('总购买数'), operate: false},
                        {field: 'quantitys_refunded', title: __('退货数'), operate: false},

                        //{field: 'handle_url', title: __('用户购买链接'), operate: false},
                        {field: 'handle_url', title: __('用户购买链接'), formatter: Table.api.formatter.url},
                        {field: 'product_images', title: __('商品主图'), formatter: Table.api.formatter.image, events: Table.api.events.image},

                        // {field: 'quantity', title: __('Quantity'), operate: 'LIKE'},
                        {field: 'variant_title', title: __('Variant_title'), operate: 'LIKE'},
                        {field: 'sku', title: __('Sku'), operate: 'LIKE'},
                        {field: 'variant_id', title: __('Variant_id'), operate: 'LIKE'},
                        {field: 'product_id', title: __('Product_id'), operate: 'LIKE'},
                        {field: 'zmaterial', title: __('采购成本（美金）'), editable: true, operate: 'BETWEEN'},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('该商品SKU库'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('该商品SKU库'),
                                    title: __('该商品SKU库'),
                                    extend: 'data-area=\'["90%","90%"]\'',
                                    classname: 'btn btn-sm btn-success btn-dialog',
                                    icon: 'fa fa-try fa-fw',
                                    url: 'feditionordersku/index?product_id={product_id}'
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('获取商品最新SKU'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('获取商品最新SKU'),
                                    title: __('获取商品最新SKU'),
                                    classname: 'btn btn-sm btn-info btn-magic btn-ajax',
                                    icon: 'fa fa-usd',
                                    confirm: '确认更新吗？',
                                    url: 'feditionorderquantity/addorderproduct?product_id={product_id}',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        // window.parent.location.reload();
                                        //刷新当前页面
                                        table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        distribution: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'feditionorderquantity/distribution',
                    add_url: 'feditionorderquantity/add',
                    edit_url: 'feditionorderquantity/edit',
                    del_url: 'feditionorderquantity/del',
                    multi_url: 'feditionorderquantity/multi',
                    import_url: 'feditionorderquantity/import',
                    table: 'fedition_orderquantity',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        // {field: 'order_id', title: __('Order_id'), operate: 'LIKE'},

                        {field: 'accounturl_name', title: __('店铺'), operate: false},
                        //{field: 'admin_name', title: __('店铺管理人'), operate: false},
                        {field: 'admin_name2', title: __('特殊归属人'), operate: false},

                        {field: 'title', title: __('Title'), operate: 'LIKE'},

                        //{field: 'handle_url', title: __('用户购买链接'), operate: false},
                        {field: 'product_images', title: __('商品主图'), formatter: Table.api.formatter.image, events: Table.api.events.image},

                        // {field: 'quantity', title: __('Quantity'), operate: 'LIKE'},
                        //{field: 'variant_title', title: __('Variant_title'), operate: 'LIKE'},
                        // {field: 'sku', title: __('Sku'), operate: 'LIKE'},
                        //{field: 'variant_id', title: __('Variant_id'), operate: 'LIKE'},
                        // {field: 'material', title: __('变体价格'), operate: 'LIKE'},
                        {field: 'product_id', title: __('Product_id'), operate: 'LIKE'},
                        //{field: 'material', title: __('变体价格'), operate: false},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});